<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="知识点 ResizeObserver：resize监听 MutationObserver:监听DOM节点的变动 MessageChannel：管道通信 InterSectionObserver：异步视口观察  ResizeObserver：resize监听window.resize下监听某DOM的改变： 123456789&amp;lt;textarea style=&amp;quot;width: 100%;&amp;">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript之观察者模式(Obverse)">
<meta property="og:url" content="http://yoursite.com/2020/12/08/JavaScript之观察者模式(Obverse)/index.html">
<meta property="og:site_name" content="春风自是人间客">
<meta property="og:description" content="知识点 ResizeObserver：resize监听 MutationObserver:监听DOM节点的变动 MessageChannel：管道通信 InterSectionObserver：异步视口观察  ResizeObserver：resize监听window.resize下监听某DOM的改变： 123456789&amp;lt;textarea style=&amp;quot;width: 100%;&amp;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-12-08T13:25:25.756Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript之观察者模式(Obverse)">
<meta name="twitter:description" content="知识点 ResizeObserver：resize监听 MutationObserver:监听DOM节点的变动 MessageChannel：管道通信 InterSectionObserver：异步视口观察  ResizeObserver：resize监听window.resize下监听某DOM的改变： 123456789&amp;lt;textarea style=&amp;quot;width: 100%;&amp;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: "",
      labels: ""
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/12/08/JavaScript之观察者模式(Obverse)/">





  <title>JavaScript之观察者模式(Obverse) | 春风自是人间客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">春风自是人间客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">是非成败转头空，青山依旧在，几度夕阳红。</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-program">
          <a href="/categories/program" rel="section">
            
            程序
          </a>
        </li>
      
        
        <li class="menu-item menu-item-reading">
          <a href="/categories/reading" rel="section">
            
            阅读
          </a>
        </li>
      
        
        <li class="menu-item menu-item-idea">
          <a href="/categories/idea" rel="section">
            
            想法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于我
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/12/08/JavaScript之观察者模式(Obverse)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="彭丹丹">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="春风自是人间客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">JavaScript之观察者模式(Obverse)</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-12-08T15:44:34+08:00">
                2020-12-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/program/" itemprop="url" rel="index">
                    <span itemprop="name">program</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,049
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          
          
        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h1><ul>
<li>ResizeObserver：resize监听</li>
<li>MutationObserver:监听DOM节点的变动</li>
<li>MessageChannel：管道通信</li>
<li>InterSectionObserver：异步视口观察</li>
</ul>
<h1 id="ResizeObserver：resize监听"><a href="#ResizeObserver：resize监听" class="headerlink" title="ResizeObserver：resize监听"></a>ResizeObserver：resize监听</h1><p>window.resize下监听某DOM的改变：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;textarea style=&quot;width: 100%;&quot; id=&quot;main&quot;&gt;&lt;/textarea&gt;</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">let mainEl = document.querySelector(&apos;#main&apos;);</span><br><span class="line">var ro = new ResizeObserver( entries =&gt; &#123;</span><br><span class="line">  console.log(entries);</span><br><span class="line">&#125;)</span><br><span class="line">ro.observe(mainEl);</span><br></pre></td></tr></table></figure>
<h1 id="MutationObserver-监听DOM节点的变动"><a href="#MutationObserver-监听DOM节点的变动" class="headerlink" title="MutationObserver:监听DOM节点的变动"></a>MutationObserver:监听DOM节点的变动</h1><h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div id=&quot;content&quot;&gt;</span><br><span class="line">      hi</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &lt;script&gt;</span><br><span class="line">      var callback = function(mutationList, observer) &#123;</span><br><span class="line">        for(var mutation of mutationList) &#123;</span><br><span class="line">          console.log(mutation)</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;;</span><br><span class="line">      var observer = new MutationObserver(callback);</span><br><span class="line">      var targetNode = document.getElementById(&apos;content&apos;);</span><br><span class="line">      observer.observe(targetNode.firstChild,&#123;</span><br><span class="line">        characterData: true</span><br><span class="line">      &#125;)</span><br><span class="line">      setTimeout(() =&gt; &#123;</span><br><span class="line">        targetNode.firstChild.data = &apos;hello&apos;</span><br><span class="line">      &#125;,3000)</span><br><span class="line">    &lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br></pre></td></tr></table></figure>
<p>3秒钟之后，id为content的DOM变成了’hello’，此时的MutationObserver就已经监听到了改变，可以进行下一步的操作。</p>
<h2 id="实现vue-nexttick"><a href="#实现vue-nexttick" class="headerlink" title="实现vue.$nexttick"></a>实现vue.$nexttick</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=&quot;en&quot;&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;title&gt;example&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;div id=&quot;app&quot;&gt;</span><br><span class="line">    &lt;div v-if=&quot;isShow&quot;&gt;</span><br><span class="line">        &lt;input type=&quot;text&quot; ref=&quot;userName&quot; /&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &lt;button @click=&quot;showInput&quot;&gt;点击显示输入框&lt;/button&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br><span class="line">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue/dist/vue.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    var app = new Vue(&#123;</span><br><span class="line">        el: &apos;#app&apos;,</span><br><span class="line">        data: &#123;</span><br><span class="line">            isShow: false</span><br><span class="line">        &#125;,</span><br><span class="line">        methods:&#123;</span><br><span class="line">            showInput()&#123;</span><br><span class="line">                this.isShow = true</span><br><span class="line">                this.mynextTick(()=&gt;&#123;</span><br><span class="line">                    this.$refs.userName.focus()</span><br><span class="line">                &#125;)</span><br><span class="line"></span><br><span class="line">            &#125;,</span><br><span class="line">            mynextTick(func)&#123;</span><br><span class="line">                var textNode = document.createTextNode(0)//新建文本节点</span><br><span class="line">                var that = this</span><br><span class="line">                var callback = function(mutationsList, observer) &#123;</span><br><span class="line">                    func.call(that);</span><br><span class="line">                    // 或</span><br><span class="line">                    // fanc();</span><br><span class="line">                &#125;</span><br><span class="line">                var observer = new MutationObserver(callback);</span><br><span class="line"></span><br><span class="line">                observer.observe(textNode,&#123;characterData:true &#125;)</span><br><span class="line">                textNode.data = 1//修改文本信息，触发dom更新</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h1 id="MessageChannel：管道通信"><a href="#MessageChannel：管道通信" class="headerlink" title="MessageChannel：管道通信"></a>MessageChannel：管道通信</h1><h2 id="MessageChannel的基本使用"><a href="#MessageChannel的基本使用" class="headerlink" title="MessageChannel的基本使用"></a>MessageChannel的基本使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const &#123;port1, port2&#125; = new MessageChannel();</span><br><span class="line">port1.onmessage = function(d) &#123;</span><br><span class="line">    console.log(`port1接收的消息是：$&#123;d.data&#125;`);</span><br><span class="line">&#125;</span><br><span class="line">port2.onmessage = function(d) &#123;</span><br><span class="line">    console.log(`port2接收的消息是：$&#123;d.data&#125;`);</span><br><span class="line">&#125;</span><br><span class="line">port1.postMessage(&apos;port1发送的消息&apos;);</span><br><span class="line">port2.postMessage(&apos;port2发送的消息&apos;);</span><br></pre></td></tr></table></figure>
<p>port1发送的由port2接收，port2发送的由port1接收。</p>
<p>也就是说，传过去的对象，接收到的时候已经不是原来的引用和指针了，这个时候再return出来，就是一个新的对象，所以肯定能实现深拷贝。</p>
<h2 id="使用MessageChannel实现深拷贝"><a href="#使用MessageChannel实现深拷贝" class="headerlink" title="使用MessageChannel实现深拷贝"></a>使用MessageChannel实现深拷贝</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">var obj = &#123;id:1,name:&#123;a:&apos;xx&apos;&#125;&#125;;</span><br><span class="line"></span><br><span class="line">function structuralClone(obj) &#123;</span><br><span class="line">    return new Promise((resolve) =&gt; &#123;</span><br><span class="line">        const &#123;port1, port2&#125; = new MessageChannel();</span><br><span class="line">        port2.onmessage = ev =&gt; resolve(ev.data);</span><br><span class="line">        port1.postMessage(obj);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line">structuralClone(obj).then(res=&gt;&#123;</span><br><span class="line">    console.log(res);</span><br><span class="line">    var obj3 = res;</span><br><span class="line">    obj3.name.a = &apos;obj3&apos;;</span><br><span class="line">    console.log(obj,obj3);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">&lt;!-- 用promise是为了好传数据 --&gt;</span><br></pre></td></tr></table></figure>
<h2 id="MessageChannel实现vue-nexttick"><a href="#MessageChannel实现vue-nexttick" class="headerlink" title="MessageChannel实现vue.$nexttick"></a>MessageChannel实现vue.$nexttick</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line"></span><br><span class="line">&lt;html lang=&quot;en-zh&quot;&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;meta charset=&quot;utf-8&quot; /&gt;</span><br><span class="line">    &lt;style type=&quot;text/css&quot;&gt;</span><br><span class="line">    &lt;/style&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;div id=&quot;app&quot;&gt;</span><br><span class="line">      &lt;div v-if=&quot;isShow&quot;&gt;</span><br><span class="line">          &lt;input type=&quot;text&quot; ref=&quot;userName&quot; /&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;button @click=&quot;showInput&quot;&gt;点击显示输入框&lt;/button&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br><span class="line"></span><br><span class="line">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue/dist/vue.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">  var app = new Vue(&#123;</span><br><span class="line">    el: &apos;#app&apos;,</span><br><span class="line">    data: &#123;</span><br><span class="line">      isShow: false</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">      showInput()&#123;</span><br><span class="line">        this.isShow = true;</span><br><span class="line">        this.myNextTick(() =&gt; &#123;</span><br><span class="line">          this.$refs.userName.focus();</span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;,</span><br><span class="line">      myNextTick(fanc)&#123;</span><br><span class="line">        var that = this;</span><br><span class="line">        const ch = new MessageChannel();</span><br><span class="line">        const port1 = ch.port1;</span><br><span class="line">        const port2 = ch.port2;</span><br><span class="line"></span><br><span class="line">        port2.onmessage = (() =&gt; &#123;</span><br><span class="line">          fanc();</span><br><span class="line">        &#125;)</span><br><span class="line">        port1.postMessage(1);</span><br><span class="line"></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h1 id="InterSectionObserver：异步视口观察"><a href="#InterSectionObserver：异步视口观察" class="headerlink" title="InterSectionObserver：异步视口观察"></a>InterSectionObserver：异步视口观察</h1><p>学习链接：</p>
<p><a href="http://www.ruanyifeng.com/blog/2016/11/intersectionobserver_api.html" target="_blank" rel="noopener">链接1</a><br><a href="https://www.jianshu.com/p/84a86e41eb2b" target="_blank" rel="noopener">链接2</a></p>
<h2 id="使用API"><a href="#使用API" class="headerlink" title="使用API"></a>使用API</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">  * 新建实例</span><br><span class="line">  * callback：回调函数</span><br><span class="line">  * option：配置项，非必填</span><br><span class="line">  */</span><br><span class="line">var io = new InterSectionObserver(callback,option);</span><br><span class="line"></span><br><span class="line">// 开始观察</span><br><span class="line">io.observe(document.getElementById(&apos;imgID&apos;));</span><br><span class="line"></span><br><span class="line">// 停止观察</span><br><span class="line">io.unobserve(element);</span><br><span class="line"></span><br><span class="line">// 关闭观察器</span><br><span class="line">io.disconnect();</span><br></pre></td></tr></table></figure>
<h2 id="返回IntersectionObserverEntry对象"><a href="#返回IntersectionObserverEntry对象" class="headerlink" title="返回IntersectionObserverEntry对象"></a>返回IntersectionObserverEntry对象</h2><p><code>IntersectionObserverEntry</code>对象提供目标元素的信息，一共有六个属性。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  time: 3893.92,</span><br><span class="line">  rootBounds: ClientRect &#123;</span><br><span class="line">    bottom: 920,</span><br><span class="line">    height: 1024,</span><br><span class="line">    left: 0,</span><br><span class="line">    right: 1024,</span><br><span class="line">    top: 0,</span><br><span class="line">    width: 920</span><br><span class="line">  &#125;,</span><br><span class="line">  boundingClientRect: ClientRect &#123;</span><br><span class="line">     // ...</span><br><span class="line">  &#125;,</span><br><span class="line">  intersectionRect: ClientRect &#123;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;,</span><br><span class="line">  intersectionRatio: 0.54,</span><br><span class="line">  target: element</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>每个属性的含义如下。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">time：可见性发生变化的时间，是一个高精度时间戳，单位为毫秒</span><br><span class="line">target：被观察的目标元素，是一个 DOM 节点对象</span><br><span class="line">rootBounds：根元素的矩形区域的信息，getBoundingClientRect()方法的返回值，如果没有根元素（即直接相对于视口滚动），则返回null</span><br><span class="line">boundingClientRect：目标元素的矩形区域的信息</span><br><span class="line">intersectionRect：目标元素与视口（或根元素）的交叉区域的信息</span><br><span class="line">intersectionRatio：目标元素的可见比例，即intersectionRect占boundingClientRect的比例，完全可见时为1，完全不可见时小于等于0</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/12/07/JavaScript之面向对象/" rel="next" title="JavaScript之面向对象">
                <i class="fa fa-chevron-left"></i> JavaScript之面向对象
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/12/08/JavaScript之运行机制/" rel="prev" title="JavaScript之运行机制">
                JavaScript之运行机制 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODk2OC8xNTQ5Ng=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" alt="彭丹丹">
            
              <p class="site-author-name" itemprop="name">彭丹丹</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">183</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#知识点"><span class="nav-number">1.</span> <span class="nav-text">知识点</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ResizeObserver：resize监听"><span class="nav-number">2.</span> <span class="nav-text">ResizeObserver：resize监听</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#MutationObserver-监听DOM节点的变动"><span class="nav-number">3.</span> <span class="nav-text">MutationObserver:监听DOM节点的变动</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本使用"><span class="nav-number">3.1.</span> <span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实现vue-nexttick"><span class="nav-number">3.2.</span> <span class="nav-text">实现vue.$nexttick</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#MessageChannel：管道通信"><span class="nav-number">4.</span> <span class="nav-text">MessageChannel：管道通信</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#MessageChannel的基本使用"><span class="nav-number">4.1.</span> <span class="nav-text">MessageChannel的基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用MessageChannel实现深拷贝"><span class="nav-number">4.2.</span> <span class="nav-text">使用MessageChannel实现深拷贝</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MessageChannel实现vue-nexttick"><span class="nav-number">4.3.</span> <span class="nav-text">MessageChannel实现vue.$nexttick</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#InterSectionObserver：异步视口观察"><span class="nav-number">5.</span> <span class="nav-text">InterSectionObserver：异步视口观察</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#使用API"><span class="nav-number">5.1.</span> <span class="nav-text">使用API</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#返回IntersectionObserverEntry对象"><span class="nav-number">5.2.</span> <span class="nav-text">返回IntersectionObserverEntry对象</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">彭丹丹</span>

  
</div>


	<div class="powered-by">
	<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
	  本站访客数:<span id="busuanzi_value_site_uv"></span>
	</span>
	</div>
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  undefined
  
  <!-- 页面爆炸效果 -->
  
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true}});</script></body>
</html>
